import { useInterval } from '@vueuse/shared';
import { ref, watch } from 'vue';

export const useSendSmsCode = (interval = 1, totalInterval = 60) => {
  const { counter, pause, resume } = useInterval(interval * 1000, {
    controls: true,
    immediate: false,
  });
  const isActive = ref(false);
  let clearUp: Function;
  const start = () => {
    counter.value = 0;
    resume();
    isActive.value = true;
    clearUp = watch(counter, (value) => {
      if (value % totalInterval === 0) {
        pause();
        isActive.value = false;
        clearUp && clearUp();
      }
    });
  };

  return {
    counter,
    start,
    isActive,
  };
};
